<?php
/**
 * 会员中心-站内信
 *
 * @copyright  Copyright (c) 2007-2013 ShopNC Inc. (http://www.shopnc.net)
 * @license    http://www.shopnc.net
 * @link       http://www.shopnc.net
 * @since      File available since Release v1.1
 */
defined('InShopNC') or exit('Access Invalid!');

class member_msgControl extends MobileControl {
	public function __construct(){
		parent::__construct();
		$this->isLogin();
	}
	
	//站内信页面
	public function showMsgOp(){
		Tpl::showpage('member_msg');
	}
	//系统消息列表
	public function systemMsgOp(){
		$model_message = new messageModel();
        $page = new Page();
        $page->setEachNum(10);
        $page->setStyle('admin');
        $message_array = $model_message->listMessage(array('from_member_id' => '0', 'message_type' => '1', 'to_member_id' => $_SESSION['member_id'], 'no_del_member_id' => $_SESSION['member_id']), $page);
        if (!empty($message_array) && is_array($message_array)) {
            foreach ($message_array as $k => $v) {
                if (!empty($v['read_member_id'])) {
                    $tmp_readid_arr = explode(',', $v['read_member_id']);
                    if (in_array($_SESSION['member_id'], $tmp_readid_arr)) {
                        $v['message_open'] = '1';
                    }
                }
                $v['from_member_name'] = '系统';
				$v['message_time'] = date('Y-m-d H:i:s',$v['message_time']);
                $message_array[$k] = $v;
            }
			echo json_encode(array('state'=>true,'message_array'=>$message_array));exit;
        }else{
			echo json_encode(array('state'=>false));exit;
		}
	}
	//私信列表
	public function privateMsgOp(){
		$model_message = new messageModel();
        $page = new Page();
        $page->setEachNum(10);
        $page->setStyle('admin');
        $message_array = $model_message->listMessage(array('message_type' => '0', 'to_member_id_common' => "{$_SESSION['member_id']}", 'no_message_state' => '2'), $page);
		if (!empty($message_array) && is_array($message_array)) {
            foreach ($message_array as $k => $v) {
				$v['message_time'] = date('Y-m-d H:i:s',$v['message_time']);
                $message_array[$k] = $v;	
			}
			echo json_encode(array('state'=>true,'message_array'=>$message_array));exit;
		}else{
			echo json_encode(array('state'=>false));exit;
		}
	
	}
	//留言列表
	public function feedbackMsgOp(){
        $model_message = new messageModel();
        $page = new Page();
        $page->setEachNum(10);
        $page->setStyle('admin');
        $message_array = $model_message->listMessage(array('message_type' => '2', 'to_member_id_common' => $_SESSION['member_id'], 'no_message_state' => '2'), $page);
		if (!empty($message_array) && is_array($message_array)) {
            foreach ($message_array as $k => $v) {
				$v['message_time'] = date('Y-m-d H:i:s',$v['message_time']);
                $message_array[$k] = $v;	
			}
			echo json_encode(array('state'=>true,'message_array'=>$message_array));exit;
		}else{
			echo json_encode(array('state'=>false));exit;
		}
	}
	//标记已读状态
	public function isReadOp(){
		$msg_id = (int)$_GET['msg_id'];
		$type_id = (int)$_GET['type_id'];
		if(!$msg_id > 0){
			echo json_encode(array('state'=>false));exit;
		}
		$sql = "SELECT * FROM #__message WHERE message_id = '$msg_id'";
		$res = Db::queryRow($sql);
		if(!empty($res) && is_array($res)){
			switch($type_id){
				case 0:
						Db::update('message',array('message_open'=>'1'),"message_id = '$msg_id'");	
						break;
				case 1:
						if($res['read_member_id'] != ''){
							$ids = $res['read_member_id'].$_SESSION['member_id'].',';
						}else{
							$ids = ','.$_SESSION['member_id'].',';
						}
						Db::update('message',array('read_member_id'=>$ids),"message_id = '$msg_id'");	
						break;
				case 2:
						Db::update('message',array('message_open'=>'1'),"message_id = '$msg_id'");	
						break;
			}
			echo json_encode(array('state'=>true));exit;
		}else{
			echo json_encode(array('state'=>false));exit;
		}
	}
	
	//取消已读状态
	public function noReadOp(){
		$msg_id = (int)$_GET['msg_id'];
		$type_id = (int)$_GET['type_id'];
		if(!$msg_id > 0){
			echo json_encode(array('state'=>false));exit;
		}
		$sql = "SELECT * FROM #__message WHERE message_id = '$msg_id'";
		$res = Db::queryRow($sql);
		if(!empty($res) && is_array($res)){
			switch($type_id){
				case 0:
						Db::update('message',array('message_open'=>'0'),"message_id = '$msg_id'");	
						break;
				case 1:
						$id_arr = explode(',',$res['read_member_id']);
						$key = array_search($_SESSION['member_id'],$id_arr);
						unset($id_arr[$key]);
						$ids = implode(',',$id_arr);
						Db::update('message',array('read_member_id'=>$ids.','),"message_id = '$msg_id'");
						break;
				case 2:
						Db::update('message',array('message_open'=>'0'),"message_id = '$msg_id'");	
						break;
			}
			echo json_encode(array('state'=>true));exit;
		}else{
			echo json_encode(array('state'=>false));exit;
		}
	}
	
	//消息删除
	public function delMsgOp(){
		$msg_id = (int)$_GET['msg_id'];
		$type_id = (int)$_GET['type_id'];
		if(!$msg_id > 0){
			echo json_encode(array('state'=>false));exit;
		}
		$sql = "SELECT * FROM #__message WHERE message_id = '$msg_id'";
		$res = Db::queryRow($sql);
		if(!empty($res) && is_array($res)){
			switch($type_id){
				case 0:
						Db::update('message',array('message_state'=>'2'),"message_id = '$msg_id'");	
						break;
				case 1:
						if($res['del_member_id'] != ''){
							$ids = $res['del_member_id'].$_SESSION['member_id'].',';
						}else{
							$ids = ','.$_SESSION['member_id'].',';
						}
						Db::update('message',array('del_member_id'=>$ids),"message_id = '$msg_id'");	
						break;
				case 2:
						Db::update('message',array('message_state'=>'2'),"message_id = '$msg_id'");	
						break;
			}
			echo json_encode(array('state'=>true));exit;
		}else{
			echo json_encode(array('state'=>false));exit;
		}
	}	

}
